#!/usr/bin/env python3
"""
检查运行所需依赖
"""

import importlib.util
import sys

REQUIRED_PACKAGES = [
    ("click", "7.0"),
    ("ruamel.yaml", "0.17.0"),
    ("jinja2", "3.0.0"),
    ("requests", "2.25.0")
]

def check_dependency(package, min_version=None):
    try:
        module = importlib.import_module(package)
        if min_version:
            version = getattr(module, "__version__", "0.0.0")
            if tuple(map(int, version.split("."))) < tuple(map(int, min_version.split("."))):
                return False, f"{package}>={min_version} (当前: {version})"
        return True, None
    except ImportError:
        return False, package

def main():
    missing = []
    for package, min_version in REQUIRED_PACKAGES:
        success, requirement = check_dependency(package, min_version)
        if not success:
            missing.append(requirement)
    
    if missing:
        print("缺少依赖包:")
        for dep in missing:
            print(f" - {dep}")
        print("\n安装命令:")
        print(f"pip install {' '.join([d.split('>')[0] for d in missing])}")
        sys.exit(1)
    else:
        print("所有依赖已安装，可以运行工具")
        sys.exit(0)

if __name__ == "__main__":
    main()
